package org.dromara.job.oms;

import cn.hutool.core.collection.CollUtil;
import com.aizuda.snailjob.client.job.core.annotation.JobExecutor;
import com.aizuda.snailjob.client.model.ExecuteResult;
import com.baomidou.lock.LockInfo;
import com.baomidou.lock.LockTemplate;
import com.baomidou.lock.executor.RedissonLockExecutor;
import com.lxk.service.core.Result;
import com.lxk.service.core.page.PageDataInfo;
import com.lxk.service.core.param.BasePageParam;
import com.lxk.service.oms.api.OrderSupplementServiceApi;
import com.lxk.service.oms.param.SupplementParam;
import com.lxk.service.oms.response.PaySupplementResult;
import com.lxk.service.setting.api.OrderConfigApi;
import com.lxk.service.setting.response.PayExpireTimeDto;
import com.lxk.service.tenant.api.TenantServiceApi;
import com.lxk.service.tenant.response.TenantDto;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.tenant.helper.TenantHelper;
import org.dromara.job.oms.contants.RedisConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 任务，查询补单
 *
 * @author huahua
 * @date 2024-08-30
 */
@Slf4j
@Component
@JobExecutor(name = "orderJobExecutor")
public class OrderJobExecutor {

    @Autowired
    private TenantServiceApi tenantServiceApi;
    @Autowired
    private OrderSupplementServiceApi supplementServiceApi;
    @Autowired
    private OrderConfigApi orderConfigApi;
    @Autowired
    private LockTemplate lockTemplate;


    /**
     * 执行补单任务
     */
    public ExecuteResult supplementJobExecute() {
        String lockKey = RedisConstants.SUPPLEMENT_JOB_EXECUTE_LOCK;
        final LockInfo lockInfo = lockTemplate.lock(lockKey, 30000L, 5000L, RedissonLockExecutor.class);
        if (null == lockInfo) {
            throw new RuntimeException("业务处理中,请稍后再试");
        }
        // 获取锁成功，处理业务
        try {
            BasePageParam param = new BasePageParam();
            param.setPageSize(10);
            doTenantPage(1, param);
        } finally {
            //释放锁
            lockTemplate.releaseLock(lockInfo);
        }
        return ExecuteResult.success("supplementJobExecute执行成功");
    }

    /**
     * 分页查询租户列表
     *
     * @param pageNum
     * @param param
     */
    private void doTenantPage(Integer pageNum, BasePageParam param) {
        param.setPageNum(pageNum);
        PageDataInfo<TenantDto> result = tenantServiceApi.page(param);
        if (result == null || CollUtil.isEmpty(result.getRows())) {
            return;
        }
        for (TenantDto tenantDto : result.getRows()) {
            try {
                TenantHelper.setDynamic(tenantDto.getTenantId());
                SupplementParam req = new SupplementParam();
                req.setPageSize(30);
                Result<List<PayExpireTimeDto>> expireTime = orderConfigApi.getPayExpireTimeConfig();
                if (expireTime.isSuccess() && CollUtil.isNotEmpty(expireTime.getData())) {
                    req.setList(expireTime.getData());
                }
                doSupplementPage(1, req);
            } finally {
                TenantHelper.clearDynamic();
            }
        }
        while (result.getHasNext()) {
            param.setPageNum(param.getPageNum() + 1);
            result = tenantServiceApi.page(param);
        }
    }

    /**
     * 分页查询补单
     *
     * @param pageNum
     * @param req
     */
    private void doSupplementPage(Integer pageNum, SupplementParam req) {
        req.setPageNum(pageNum);
        PaySupplementResult paySupplementResult = supplementServiceApi.supplement(req);
        while (paySupplementResult.getHasNext()) {
            req.setPageNum(req.getPageNum() + 1);
            paySupplementResult = supplementServiceApi.supplement(req);
        }
    }
}
